package com.example.spring.jpa.specification;

import com.example.spring.jpa.ast.PriorityOperation;

import javax.persistence.criteria.*;
import java.text.ParseException;

/**
 * @author: orange
 * @create: 2023-07-24 20:59
 * @description:
 */
public abstract class GlobalPredicate implements PredicateGenerate {
    @Override
    public Predicate generate(GenerateApplicationContext context) throws ParseException {
        if (context.getRight() == null) {
            if (PriorityOperation.IsNotNull.getOperation().equals(context.getOperation())) {
                return context.getBuilder().isNotNull(getExpression(context));
            } else if (PriorityOperation.IsNull$2.getOperation().equals(context.getOperation())
                    || PriorityOperation.IsNull$1.getOperation().equals(context.getOperation())) {
                return context.getBuilder().isNull(getExpression(context));
            }
        }
        return doGenerate(context);
    }

    public abstract Expression<?> getExpression(GenerateApplicationContext context);

    public abstract Predicate doGenerate(GenerateApplicationContext context) throws ParseException;
}
